package com.awspaass.user.apps.exchangecenterreservation;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.shhtaerospaceindustrial.sms.SmsUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;
/*
 * 交流中心餐饮预订CMD
 */
@Controller
public class AppCmdCateringReservation {
	/*
	 * 交流中心销售取消已接单订单
	 * 0:未提交|1:已提交|2:已接单|3:已取消
	 */
	@Mapping("com.awspaass.user.apps.CateringReservation_saleCancelAssign")
	public String saleCancelAssign(String bindid ,String reason,UserContext uc) {
		JSONObject returnData = new JSONObject();
		String TELEPHONE =uc.getUserModel().getMobile();//节点办理人电话
		String GUANLIYUAN =uc.getUserModel().getUserName();//办理人名称
		if(null == bindid || "".equals(bindid)  ) {
			returnData.put("status", "0");
			returnData.put("message", "请检查参数!");
			return returnData.toString();
		}
		try {
			String querySql = "select EATTINGSTARTDATE,ORDERSEL,APPLYNAME,APPLYUSERCELLPHONE,ROOMNUM,XSCOMPANY ,YCDATE  from  VIEW_CHECKRESTAURANT WHERE BINDID = " + "'"
					+ bindid + "'";
			String ORDERSEL = CoreUtil.objToStr(DBSql.getString(querySql, "ORDERSEL"));// 用餐时段
			String ycst;
			if("0".equals(ORDERSEL)) {//0:午餐|1:晚餐
				 ycst ="午餐";
			}else {
				 ycst ="晚餐";
			}
			String YCDATE = CoreUtil.objToStr(DBSql.getString(querySql, "YCDATE"));// 用餐时间
			String APPLYNAME = CoreUtil.objToStr(DBSql.getString(querySql, "APPLYNAME"));// 联系人
			String APPLYUSERCELLPHONE = CoreUtil.objToStr(DBSql.getString(querySql, "APPLYUSERCELLPHONE"));// 联系电话
			String ROOMNUM=""; 
			 ROOMNUM = CoreUtil.objToStr(DBSql.getString(querySql, "ROOMNUM"));// 预定房间号
			 if("".equals(ROOMNUM)) {
				 ROOMNUM="交流中心"; 
			 }
			String message = "{'APPLYNAME':'" + APPLYNAME + "','EATTINGSTARTDATE':'" + YCDATE + "','ORDERSEL':'" + ycst
					+ "','ROOMNUM':'" + ROOMNUM + "','GUANLIYUAN':'" + GUANLIYUAN + "','TELEPHONE':'" + TELEPHONE + "'}";
			String updateAssignSql = "update BO_EU_SH_JLCENTER_RESTAURANT set ORDERSTATUS='3',FORCEDCANCEL='"+reason+"' where BINDID ='" + bindid + "'";
			DBSql.update(updateAssignSql);
			SmsUtil.sendSms(APPLYUSERCELLPHONE, "SMS_249245797", message);
			returnData.put("status", "0");
			returnData.put("message", "取消成功！");
		} catch (Exception e) {
			returnData.put("status", "1");
			returnData.put("message", e.getMessage());
			e.printStackTrace();

		}
		return returnData.toString();

	}
	/*
	 * 加载用户餐饮预订完成的交流中心销售已接单订单的所有订单
	 */
	@Mapping("com.awspaass.user.apps.CateringReservation_saleDetails")
	public static String getCateringReservation_saleDetails(UserContext uc,String ycbegin,String ycend,String ycperson) {
		JSONObject result = new JSONObject();
		String checksql="";
		String sql="";
		if(!"".equals(ycbegin)) {//用餐时间大于等于
			sql = " and t.YCDATE >='" + ycbegin + "' ";
			checksql +=sql;
		}
		if(!"".equals(ycend)) { //用餐时间小于等于
			sql =" and t.YCDATE <='" + ycend + "' ";
			checksql +=sql;
		}
		if(!"".equals(ycperson)) { //订餐人
			sql =" and t.APPLYNAME like '%" + ycperson + "%' ";
			checksql +=sql;
		}
		try {
			if(!"".equals(checksql)) {
				 String querySql ="select t.APPLYNAME,t.EATTINGSTARTDATE,t.ORDERSEL,t.ROOMNUM,t.ORDERSTATUS,t.BINDID,t.YCDATE  from  VIEW_CHECKRESTAURANT  t  "
				 		+ "where t.ORDERSTATUS in  ('2') "+ checksql+ "  order by  t.YCDATE  asc";
				 List<Map<String, Object>> urlList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
					if (urlList == null || urlList.isEmpty()) {
						JSONArray itemArray = new JSONArray();
						result.put("status", "0");
						result.put("item", itemArray);
					}
					JSONArray itemArray = new JSONArray();
					for (int i = 0; i < urlList.size(); i++) {
						Map<String, Object> urlMap = urlList.get(i);
						JSONObject item = new JSONObject();
						String APPLYNAME = CoreUtil.objToStr(urlMap.get("APPLYNAME"));// 预定人
						String YCDATE = CoreUtil.objToStr(urlMap.get("YCDATE"));// 用餐日期
						String ORDERSEL = CoreUtil.objToStr(urlMap.get("ORDERSEL"));// 用餐时段 0：午餐 1：晚餐
						String ORDERSTATUS = CoreUtil.objToStr(urlMap.get("ORDERSTATUS"));// 订单状态
						String ROOMNUM = CoreUtil.objToStr(urlMap.get("ROOMNUM"));// 房间号
						String BINDID = CoreUtil.objToStr(urlMap.get("BINDID"));// 流程实例id
						item.put("applyname", APPLYNAME);	
						item.put("ycdate", YCDATE);
						item.put("ordersel", ORDERSEL);
						item.put("roomnum", ROOMNUM);
						item.put("bindid", BINDID);
						item.put("orderstatus", ORDERSTATUS);
						itemArray.add(item);
					 }
					result.put("status", "0");
					result.put("item", itemArray);
				    
			}else {
				Date now = new Date();// 系统日期
				SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd");
				String today = DateFormat.format(now);// 日期
				Calendar calendar = Calendar.getInstance();  
			    calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 6);  
			    Date endtoday = calendar.getTime();  
			    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
			    String enddate = format.format(endtoday);  
				String querySql = "select t.APPLYNAME,t.EATTINGSTARTDATE,t.ORDERSEL,t.ROOMNUM,t.ORDERSTATUS,t.BINDID,t.YCDATE  from  VIEW_CHECKRESTAURANT  t  where t.ORDERSTATUS in  ('2') "
						+ " and  t.YCDATE >=  '" + today + "' and t.YCDATE  <='" + enddate + "' order by  t.YCDATE  asc";
				List<Map<String, Object>> urlList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
				if (urlList == null || urlList.isEmpty()) {
					JSONArray itemArray = new JSONArray();
					result.put("status", "0");
					result.put("item", itemArray);
				}
				JSONArray itemArray = new JSONArray();
				for (int i = 0; i < urlList.size(); i++) {
					Map<String, Object> urlMap = urlList.get(i);
					JSONObject item = new JSONObject();
					String APPLYNAME = CoreUtil.objToStr(urlMap.get("APPLYNAME"));// 预定人
					String YCDATE = CoreUtil.objToStr(urlMap.get("YCDATE"));// 用餐日期
					String ORDERSEL = CoreUtil.objToStr(urlMap.get("ORDERSEL"));// 用餐时段 0：午餐 1：晚餐
					String ORDERSTATUS = CoreUtil.objToStr(urlMap.get("ORDERSTATUS"));// 订单状态
					String ROOMNUM = CoreUtil.objToStr(urlMap.get("ROOMNUM"));// 房间号
					String BINDID = CoreUtil.objToStr(urlMap.get("BINDID"));// 流程实例id
					item.put("applyname", APPLYNAME);
					item.put("ycdate", YCDATE);
					item.put("ordersel", ORDERSEL);
					item.put("roomnum", ROOMNUM);
					item.put("bindid", BINDID);
					item.put("orderstatus", ORDERSTATUS);
					itemArray.add(item);
				
				 }
				result.put("status", "0");
				result.put("item", itemArray);
		  }
		} catch (Exception e) {

			result.put("status", "1");
			result.put("message", e.getMessage());
			e.printStackTrace();
		}

		return result.toString();
	}

	/*
	 * 用户取消已接单订单  
	 */
	@Mapping("com.awspaass.user.apps.CateringReservation_userCancelAssign")
	public String userCancelAssign(String bindid,String reason, UserContext uc) {
		JSONObject returnData = new JSONObject();
		String TELEPHONE =uc.getUserModel().getMobile();//节点办理人电话
		String GUANLIYUAN =uc.getUserModel().getUserName();//办理人名称
		if(null == bindid || "".equals(bindid)  ) {
			returnData.put("status", "0");
			returnData.put("message", "请检查参数!");
			return returnData.toString();
		}
		
		try {
			String querySql = "select EATTINGSTARTDATE ,ORDERSEL ,APPLYNAME ,APPLYUSERCELLPHONE,ROOMNUM,ORDERSTATUS "
					+ ",APPLYUID,XSCOMPANY ,YCDATE  from  VIEW_CHECKRESTAURANT  WHERE BINDID ='" + bindid + "'";
			String ORDERSEL = CoreUtil.objToStr(DBSql.getString(querySql, "ORDERSEL"));// 用餐时段
			String ycst;
			if("0".equals(ORDERSEL)) {//0:午餐|1:晚餐
				 ycst ="午餐";
			}else {
				 ycst ="晚餐";
			}
			String YCDATE = CoreUtil.objToStr(DBSql.getString(querySql, "YCDATE"));// 用餐时间
			String APPLYNAME = CoreUtil.objToStr(DBSql.getString(querySql, "APPLYNAME"));// 联系人
			String APPLYUSERCELLPHONE = CoreUtil.objToStr(DBSql.getString(querySql, "APPLYUSERCELLPHONE"));// 联系电话
			String ROOMNUM=""; 
			 ROOMNUM = CoreUtil.objToStr(DBSql.getString(querySql, "ROOMNUM"));// 预定房间号
			 if("".equals(ROOMNUM)) {
				 ROOMNUM="交流中心"; 
			 }
			String ORDERSTATUS = CoreUtil.objToStr(DBSql.getString(querySql, "ORDERSTATUS"));// 订单状态
			String APPLYUID = CoreUtil.objToStr(DBSql.getString(querySql, "APPLYUID"));// 订餐人ID
			String XSCOMPANY = CoreUtil.objToStr(DBSql.getString(querySql, "XSCOMPANY"));// 预订单位
			String stquerySql="select  t.applyusername, t.telphone from BO_EU_SH_XSFZDW t  where t.applycompanyname = '"+XSCOMPANY+"'";
			String applyusername = CoreUtil.objToStr(DBSql.getString(stquerySql, "applyusername"));//销售管理人员姓名
			String[] name =applyusername.split(" ");
			String guanliyuan="";
			if(!"".equals(name[0])) {
				guanliyuan=name[0];
			}else {
				guanliyuan="管理员";
			}
			String telphone = CoreUtil.objToStr(DBSql.getString(stquerySql, "telphone"));//销售管理人员电话号
			String message = "{'APPLYNAME':'" + APPLYNAME + "','EATTINGSTARTDATE':'" + YCDATE + "','ORDERSEL':'" + ycst
					+ "','ROOMNUM':'" + ROOMNUM + "'}";
			String messageguliyuan = "{'GUANLIYUAN':'" + guanliyuan + "','XSCOMPANY':'" + XSCOMPANY + "','APPLYNAME':'" + APPLYNAME
					+ "','EATTINGSTARTDATE':'" + YCDATE + "','ORDERSEL':'" + ycst + "','ROOMNUM':'" + ROOMNUM + "'}";
			String updateAssignSql = "update BO_EU_SH_JLCENTER_RESTAURANT set ORDERSTATUS='3',FORCEDCANCEL='"+reason+"'  where BINDID ='" + bindid + "'";
			DBSql.update(updateAssignSql);
			if("1".equals(ORDERSTATUS)) {
				try {
					System.out.println("准备终止流程！流程号:" + bindid + "用户ID:" + APPLYUID);
					SDK.getProcessAPI().terminateById(bindid, APPLYUID);
				} catch (Exception e) {
					e.printStackTrace();
				}
				SmsUtil.sendSms(APPLYUSERCELLPHONE, "SMS_249310805", message);//取消成功给用户发消息
				
			}else {
				SmsUtil.sendSms(APPLYUSERCELLPHONE, "SMS_249310805", message);//取消成功给用户发消息
				String[] telephonenum =telphone.split("、");
				if(telephonenum.length > 0) {
					for (int i = 0; i < telephonenum.length; i++) {
						SmsUtil.sendSms(telephonenum[i], "SMS_249250799", messageguliyuan);//取消成功给调度发消息
					}
				}
				
			}
			returnData.put("status", "0");
			returnData.put("message", "取消成功！");
		} catch (Exception e) {
			returnData.put("status", "1");
			returnData.put("message", e.getMessage());
			e.printStackTrace();
		}

		return returnData.toString();

	}

	/*
	 * 加载用户预订完成的可取消的所有订单
	 */
	@Mapping("com.awspaass.user.apps.CateringReservation_userDetails")
	public static String getCateringReservation_userDetails(UserContext uc) {
		String userId = uc.getUID();
		JSONObject result = new JSONObject();
		try {
			Date now = new Date();// 系统日期
			SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String today = DateFormat.format(now);// 日期
			String querySql = "select t.EATTINGSTARTDATE,t.ORDERSEL,t.ROOMNUM,t.ORDERSTATUS,t.BINDID,t.YCDATE  from  VIEW_CHECKRESTAURANT  t  where t.ORDERSTATUS in  ('1','2') "
					+ " and  t.YCDATE >=  '" + today + "' and t.APPLYUID ='" + userId + "' order by  t.BDATE  asc";
			List<Map<String, Object>> urlList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
			if (urlList == null || urlList.isEmpty()) {
				JSONArray itemArray = new JSONArray();
				result.put("status", "0");
				result.put("item", itemArray);
			}
			JSONArray itemArray = new JSONArray();
			for (int i = 0; i < urlList.size(); i++) {
				Map<String, Object> urlMap = urlList.get(i);
				JSONObject item = new JSONObject();
				String EATTINGSTARTDATE = CoreUtil.objToStr(urlMap.get("EATTINGSTARTDATE"));// 用餐时间
				String YCDATE = CoreUtil.objToStr(urlMap.get("YCDATE"));// 用餐日期
				Date YCXXDATE = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(EATTINGSTARTDATE.replace("/", "-"));//格式转换
				String ORDERSEL = CoreUtil.objToStr(urlMap.get("ORDERSEL"));// 用餐时段 0：午餐 1：晚餐
				String ORDERSTATUS = CoreUtil.objToStr(urlMap.get("ORDERSTATUS"));// 订单状态
				String ROOMNUM = CoreUtil.objToStr(urlMap.get("ROOMNUM"));// 房间号
				String BINDID = CoreUtil.objToStr(urlMap.get("BINDID"));// 流程实例id
				String PACKAGESTANDARD = CoreUtil.objToStr(urlMap.get("PACKAGESTANDARD"));// 用餐标准
				if ( YCXXDATE.getTime()- now.getTime() >= 24 * 60 * 60 * 1000  && "3".equals(PACKAGESTANDARD)) {
					item.put("ycdate", YCDATE);
					item.put("ordersel", ORDERSEL);
					item.put("roomnum", ROOMNUM);
					item.put("bindid", BINDID);
					item.put("orderstatus", ORDERSTATUS);
					itemArray.add(item);
				} else if (!"3".equals(PACKAGESTANDARD) && YCXXDATE.getTime()- now.getTime()  >= 4 * 60 * 60 * 1000) {
					item.put("ycdate", YCDATE);
					item.put("ordersel", ORDERSEL);
					item.put("roomnum", ROOMNUM);
					item.put("bindid", BINDID);
					item.put("orderstatus", ORDERSTATUS);
					itemArray.add(item);
				}

			}
			result.put("status", "0");
			result.put("item", itemArray);
		} catch (Exception e) {

			result.put("status", "1");
			result.put("message", e.getMessage());
			e.printStackTrace();
		}

		return result.toString();
	}
}
